import { BodySetup } from "utils/creep/CreepSetup";
import { ActionBase, ActionBaseOfCreep } from "../ActionBase";
import { EActionCode } from "framework/FSM/Action/ActionCode";

/**
 * 升级Action
 */
export class ActionRepair  extends ActionBaseOfCreep<IActionCMD<"repair">>
{
    protected updateArgs(action: IActionCMD<"repair">): EActionCode 
    {
        if(super.updateArgs(action) !== EActionCode.OK)
        {
            return EActionCode.FAILED;
        }
        if(action.arg.entity.store.getUsedCapacity(RESOURCE_ENERGY) == 0)
        {
            return EActionCode.FAILED;
        }
        action.arg.roomObject = Game.getObjectById(action.arg.roomObject.id) as Structure;
        if(!action.arg.roomObject)
        {
            return EActionCode.FAILED;
        }
       
        return EActionCode.OK;
    }
    protected _execute(action: IActionCMD<"repair">): ActionCode 
    {
        const structure = action.arg.roomObject;
        if(action.arg.goalhit)
        {
            if(structure.hits >=action.arg.goalhit)
            {
                return EActionCode.OK;
            }
        }
        else 
        {
            if(structure.hits == structure.hitsMax)
            {
                return EActionCode.OK;
            }
        }
        if(structure.pos.distance(action.arg.entity.pos)>3)
        {
            action.arg.entity.mymoveTo({pos:structure.pos,range:3},{});
        }
        else
        {
            action.arg.entity.repair(structure);
            
        }
        return EActionCode.RUNNING
    }
}